package com.fansl.koala.quickdev.module.users.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fansl.koala.quickdev.common.bean.PageBean;
import com.fansl.koala.quickdev.common.bean.R;
import com.fansl.koala.quickdev.common.constants.CommonConstants;
import com.fansl.koala.quickdev.module.users.entity.SysUserMenu;
import com.fansl.koala.quickdev.module.users.service.ISysUserMenuService;
import com.fansl.koala.quickdev.module.users.vo.SysUserMenuVo;
import com.fansl.koala.quickdev.module.users.bean.dto.SysUserMenuDTO;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fansl
 * @since 2020-04-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/users/sys-user-menu")
public class SysUserMenuController {
    private final ISysUserMenuService sysUserMenuService;

    /**
    * 添加
    * @param sysUserMenuDTO 参数
    * @return true/false
    */
    @PostMapping("")
    public Mono<R<Boolean>> add(@RequestBody SysUserMenuDTO sysUserMenuDTO) {
        SysUserMenu sysUserMenu = sysUserMenuDTO2Bean(sysUserMenuDTO);
        boolean b = sysUserMenuService.save(sysUserMenu);
        return Mono.just(R.ok(b));
    }

    /**
    * 根据主键更新
    * @param sysUserMenuDTO 参数
    * @return true/false
    */
    @PutMapping("")
    public Mono<R<Boolean>> updateById(@RequestBody SysUserMenuDTO sysUserMenuDTO) {
        SysUserMenu sysUserMenu = sysUserMenuDTO2Bean(sysUserMenuDTO);
        boolean b = sysUserMenuService.updateById(sysUserMenu);
        return Mono.just(R.ok(b));
    }

    /**
    * 根据主键获取信息
    * @param id 主键
    * @return 查询数据
    */
    @GetMapping("/{id}")
    public Mono<R<SysUserMenuVo>> getById(@PathVariable Long id) {
        SysUserMenu sysUserMenu = sysUserMenuService.getById(id);
        SysUserMenuVo sysUserMenuVo = sysUserMenu2Vo(sysUserMenu);
        return Mono.just(R.ok(sysUserMenuVo));
    }

    /**
    * 根据主键删除
    * @param id 主键
    * @return true/false
    */
    @DeleteMapping("/{id}")
    public Mono<R<Boolean>> delById(@PathVariable Long id) {
       boolean b = sysUserMenuService.removeById(id);
       return Mono.just(R.ok(b));
    }

    /**
    * 分页查询
    * @param page 分页询参数
    * @param sysUserMenu 查询参数
    * @return 查询数据
    */
    @GetMapping("/page")
    public Mono<R<PageBean<SysUserMenuVo>>> page(PageBean<SysUserMenuVo> page, SysUserMenu sysUserMenu) {
        //分页参数
        Page<SysUserMenu> queryPage = new Page<>();
        queryPage.setCurrent(page.getPageNo());
        queryPage.setSize(page.getPageSize());
        if (StrUtil.isNotBlank(page.getSortOrder()) && StrUtil.isNotBlank(page.getSortField())) {
            OrderItem orderItem = new OrderItem();
            orderItem.setAsc(page.getSortOrder().equals(CommonConstants.PAGE_QUERY_ORDER_ASC));
            orderItem.setColumn(StrUtil.toUnderlineCase(page.getSortField()));
            queryPage.setOrders(Collections.singletonList(orderItem));
        }
        //查询
        LambdaQueryWrapper<SysUserMenu> queryWrapper = Wrappers.lambdaQuery(sysUserMenu);
        Page<SysUserMenu> result = sysUserMenuService.page(queryPage, queryWrapper);
        //结果转vo
        List<SysUserMenuVo> data = result.getRecords().stream()
            .map(this::sysUserMenu2Vo)
            .collect(Collectors.toList());
        //转换结果
        page.setData(data);
        page.setTotalCount(result.getTotal());
        page.setTotalPage(result.getPages());
        return Mono.just(R.ok(page));
    }

    /**
    * Vo对象转换
    * @param sysUserMenu 源对象
    * @return 目标对象
    */
    private SysUserMenuVo sysUserMenu2Vo(SysUserMenu sysUserMenu){
        SysUserMenuVo target = new SysUserMenuVo();
        BeanUtil.copyProperties(sysUserMenu,target);
        return target;
    }

    /**
    * DTO对象转换
    * @param sysUserMenuDTO 源对象
    * @return 目标对象
    */
    private SysUserMenu sysUserMenuDTO2Bean(SysUserMenuDTO sysUserMenuDTO){
        SysUserMenu target = new SysUserMenu();
        BeanUtil.copyProperties(sysUserMenuDTO,target);
        return target;
    }
}
